<?php

namespace app\admin\controller\data;

use app\admin\model\money\Platform;
use app\admin\model\Shop;
use app\admin\model\shop\Withdraw;
use app\common\controller\Backend;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Shoptotaldata extends Backend
{

    /**
     * Shoptotaldata模型对象
     * @var \app\admin\model\data\Shoptotaldata
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\data\Shoptotaldata;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index_1()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['shop'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'shop_id', 'shop_name', 'create_time', 'book_amount', 'book_pay_amount', 'charge_amount', 'tuan_amount', 'dou_amount', 'mt_amount', 'profit', 'refund_amount', 'balance_amount', 'tq_amount', 'total_amount', 'order_tuan']);
                $row->visible(['shop']);
                $row->getRelation('shop')->visible(['open_date']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if ($sort == 'id') {
                $sort = 'total_amount';
            }

            $params = $this->request->get();
            $filter = json_decode($params['filter'], true);
            $where2 = array();
            if (array_key_exists('create_time', $filter)) {
                $create_time = $filter['create_time'];
                $timeParts = explode(' - ', $create_time);
                $st = strtotime($timeParts[0]);
                $et = strtotime($timeParts[1]);
                $where2['createtime'] = array('between', [$st, $et]);
            }
            if (array_key_exists('shop_name', $filter)) {
                $shop_name = $filter['shop_name'];
                $shopModel = new Shop();
                $shopInfo = $shopModel->where('name', 'like', '%' . $shop_name . '%')->field('id')->select();
                if (empty($shopInfo)) {
                    $shop_ids = [999999999999999999999];
                } else {
                    $shop_ids = [];
                    foreach ($shopInfo as $shop) {
                        $shop_ids[] = $shop['id'];
                    }
                }
                $where2['shop_id'] = array('in', $shop_ids);
            }

            $list = $this->model
                ->with(['shop'])
                ->field('shop_id,shop_name,sum(total_amount) as total_amount,sum(book_amount) as book_amount,sum(tq_amount) as tq_amount,sum(book_pay_amount) as book_pay_amount,sum(charge_amount) as charge_amount,sum(tuan_amount) as tuan_amount,sum(dou_amount) as dou_amount,sum(mt_amount) as mt_amount,sum(profit) as profit,sum(refund_amount) as refund_amount,sum(balance_amount) as balance_amount,sum(order_tuan) as order_tuan')
                ->where($where)
                ->group('shop_id')
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['id', 'shop_id', 'shop_name', 'book_amount', 'book_pay_amount', 'charge_amount', 'tuan_amount', 'dou_amount', 'mt_amount', 'profit', 'refund_amount', 'balance_amount', 'tq_amount', 'total_amount', 'order_tuan']);
                $row->visible(['shop']);
                $row->getRelation('shop')->visible(['open_date']);
            }

            //查询指定公司收益
            $platformModel = new Platform();
            $shopWithdrawModel = new Withdraw();

            $platformData = $platformModel->where($where2)->sum('rate');//统计平台收益
            $shopProfit = $platformModel->where($where2)->sum('shop_amount');//统计商家收益
            $total_pay = $platformModel->where($where2)->sum('order_pay_amount');//统计平台小程序支付金额

            $totalSim = $shopWithdrawModel->where($where2)->where('type',2)->sum('amount');//统计SIM卡扣费
            $totalZero= $shopWithdrawModel->where($where2)->where('type',1)->sum('amount');//统计0抽佣扣费
            $totalMtDou = $shopWithdrawModel->where($where2)->where('type',0)->sum('amount');//统计美团抖音抽佣扣费

            $platformData=formatMoney($platformData + $totalSim +$totalZero + $totalMtDou);

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend" => ['one' => $total_pay, 'two' => $shopProfit, 'three' => $platformData]);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function show1(){
        $params = $this->request->get();

        $where2 = array();
        $shop_name = $params['shop_name'];
        if(!empty($shop_name)){
            $shopModel = new Shop();
            $shopInfo = $shopModel->where('name', 'like', '%' . $shop_name . '%')->field('id')->select();
            if (empty($shopInfo)) {
                $shop_ids = [999999999999999999999];
            } else {
                $shop_ids = [];
                foreach ($shopInfo as $shop) {
                    $shop_ids[] = $shop['id'];
                }
            }
            $where2['shop_id'] = array('in', $shop_ids);
        }

        $times = $params['time'];
        if(!empty($times)){
            $timeArr = explode(' - ',$times);
            $st = strtotime($timeArr[0]);
            $et = strtotime($timeArr[1]);

            $where2['createtime'] = array('between', [$st, $et]);
        }

        //查询指定公司收益
        $platformModel = new Platform();
        $shopWithdrawModel = new Withdraw();

        $platformQp = $platformModel->where($where2)->where('type','order')->where('from','qp')->sum('rate');//统计平台棋牌收益
        $platformTq = $platformModel->where($where2)->where('type','order')->where('from','tq')->sum('rate');//统计平台台球收益
        $platformCharge = $platformModel->where($where2)->where('type','charge')->where('from','qp')->sum('rate');
        $platformTuan = $platformModel->where($where2)->where('type','tuan')->where('from','qp')->sum('rate');

        $totalSim = $shopWithdrawModel->where($where2)->where('type',2)->sum('amount');//统计SIM卡扣费
        $totalZero= $shopWithdrawModel->where($where2)->where('type',1)->sum('amount');//统计0抽佣扣费
        $totalMtDou = $shopWithdrawModel->where($where2)->where('type',0)->sum('amount');//统计美团抖音抽佣扣费

        $totalPlatform = formatMoney($platformQp + $platformTq + $platformCharge + $platformTuan+ $totalSim +$totalZero + $totalMtDou);
        $data = [
            'qp'=>$platformQp,
            'tq'=>$platformTq,
            'charge'=>$platformCharge,
            'tuan'=>$platformTuan,
            'sim'=>$totalSim,
            'zero'=>$totalZero,
            'mtDou'=>$totalMtDou,
            'total'=>$totalPlatform,
        ];

        $this->assign('data',$data);

        return $this->view->fetch();
    }

}
